﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Web.Security;
namespace John
{
    public partial class joinvip : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                setSelectClass();
                csf();
            }

        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (uploadFile.PostedFile.FileName.Trim() != String.Empty && (Path.GetExtension(uploadFile.PostedFile.FileName) != ".gif" && Path.GetExtension(uploadFile.PostedFile.FileName) != ".jpg"))
            {
                Response.Write("<Script>alert('上传的图片格式必须为.gif或.jpg!!')</Script>");
                return;
            }
            try
            {
                if (uploadFile.PostedFile.FileName.Trim() != String.Empty)
                {
                    string nam = uploadFile.PostedFile.FileName;
                    //取得文件名(抱括路径)里最后一个"."的索引
                    int i = nam.LastIndexOf(".");
                    //取得文件扩展名
                    string newext = nam.Substring(i);
                    //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
                    DateTime now = DateTime.Now;
                    string newname = now.DayOfYear.ToString() + uploadFile.PostedFile.ContentLength.ToString();
                    uploadFile.PostedFile.SaveAs(Server.MapPath(".\\userimg\\" + newname + newext));
                    string imgname = newname + newext;                   
                    string pws;//密码加密
                    pws = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text.Trim(), "MD5");
                    string sql;
                    sql = "insert into Muser(hyname,pws,sex,zsname,zhiwei,chuanzhen,guding,email,adreess,youbian,gsleixing,gsname,gsdiqu,gshangye,gsjs,cimg) values('" + TextBox1.Text.Trim() + "','" + pws + "','" + Radio.SelectedValue.Trim() + "','" + TextBox7.Text.Trim() + "','" + TextBox9.Text.Trim() + "','" + TextBox10.Text.Trim() + "','" + TextBox11.Text.Trim() + "','" + TextBox4.Text.Trim() + "','" + TextBox12.Text.Trim() + "','" + TextBox5.Text.Trim() + "','" + DropDownList1.SelectedValue.Trim() + "','" + TextBox5.Text.Trim() + "','" + DropDownList3.SelectedValue.Trim() + "','" + DropDownList2.SelectedValue + "','" + txtBewrite.Text.Trim() + "','" + imgname+ "')";
                    DBConn mydb = new DBConn();
                    mydb.Insert(sql);
                    mydb.Close();
                    Response.Redirect("sucess.aspx");
                }
                else
                {
                    string pws;//密码加密
                    pws = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text.Trim(), "MD5");
                    string sql;
                    sql = "insert into Muser(hyname,pws,sex,zsname,zhiwei,chuanzhen,guding,email,adreess,youbian,gsleixing,gsname,gsdiqu,gshangye,gsjs,cimg) values('" + TextBox1.Text.Trim() + "','" + pws + "','" + Radio.SelectedValue.Trim() + "','" + TextBox7.Text.Trim() + "','" + TextBox9.Text.Trim() + "','" + TextBox10.Text.Trim() + "','" + TextBox11.Text.Trim() + "','" + TextBox4.Text.Trim() + "','" + TextBox12.Text.Trim() + "','" + TextBox5.Text.Trim() + "','" + DropDownList1.SelectedValue.Trim() + "','" + TextBox5.Text.Trim() + "','" + DropDownList3.SelectedValue.Trim() + "','" + DropDownList2.SelectedValue + "','" + txtBewrite.Text.Trim() + "','showimg.gif')";
                     DBConn mydb = new DBConn();
                    mydb.Insert(sql);
                    mydb.Close();
                    Response.Redirect("sucess.aspx");
                }
            }
            catch
            {
                //Response.Redirect("Error.aspx");

            }           
        }
        private void setSelectClass()//绑定 类别选框
        {
            DBConn myDB = new DBConn();
            string mySql = "select CID,CName from Category";
            DropDownList2.DataSource = myDB.getDataReader(mySql);
            DropDownList2.DataTextField = "CName";
            DropDownList2.DataValueField = "CID";
            DropDownList2.DataBind();
            myDB.Close();
        }
        private void csf()//绑定 省份
        {
            DBConn myDB = new DBConn();
            string mySql = "select ID,CName from csf";
            DropDownList3.DataSource = myDB.getDataReader(mySql);
            DropDownList3.DataTextField = "CName";
            DropDownList3.DataValueField = "ID";
            DropDownList3.DataBind();
            myDB.Close();
        }
    }
}
